package top.infopub.mgr.cap.web;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.cap.RepaymentScheduleDto;
import top.infopub.dto.mkt.LoanContractDto;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.cap.domain.DropLoanFinDomain;
import top.infopub.mgr.cap.service.CapProcessManageService;
import top.infopub.mgr.cap.service.InformationSubmitApprovalService;
import top.infopub.mgr.cap.service.LoanContractService;
import top.infopub.mgr.cap.service.RepaymentScheduleService;
import top.infopub.mgr.cap.vo.RepaymentScheduleInfoVo;
import top.infopub.mgr.cap.vo.RepaymentScheduleVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.model.cap.InformationSubmitApproval;
import top.infopub.model.cap.LoanContract;
import top.infopub.model.cap.LoanContractSublist;
import top.infopub.model.cap.RepaymentSchedule;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author Warking
 * @Date 2021/5/24 10:32
 * @Version 1.0 还款计划*
 */
@Controller
@RequestMapping("/repaymentSchedule")
public class RepaymentScheduleController {
    @Autowired
    private RepaymentScheduleService repaymentScheduleService;
    @Autowired
    private LoanContractService loanContractService;

    @Autowired
    private CapProcessManageService capProcessManageService;

    @RequestMapping("/list")
    public String list() {
        return "cap/lay-repaymentSchedule-list";
    }
    /*还款计划列表接口*/
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<RepaymentSchedule> queryDataByCondition(RepaymentSchedule repaymentSchedule, ParamPage paramPage) throws ParseException {
        Page<RepaymentSchedule> page = new Page();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //处理时间范围
        if (!StringUtils.isEmpty(repaymentSchedule.getDates())) {
            String[] splitTime = repaymentSchedule.getDates().split("~");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            repaymentSchedule.setStartDate(format.parse(splitTime[0]));
            repaymentSchedule.setEndDate(format.parse(splitTime[1]));
        }
        page = repaymentScheduleService.selectPage(page, repaymentSchedule);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    //跳转详情
    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetail(HttpServletRequest request, String id, String proCode) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("proCode", proCode);
        request.setAttribute("id", id);
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");
        if (!StringUtils.isEmpty(id)) {
            RepaymentSchedule repaymentSchedule = repaymentScheduleService.findById(id);
            if (repaymentSchedule != null) {
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), repaymentSchedule.getProcessStatus())) {
                    request.setAttribute("isAuditFlag", "0");
                }
                if (StringUtils.isNotBlank(repaymentSchedule.getProcessId()) && repaymentSchedule.getProcessId().length() > 10) {
                    request.setAttribute("processId", repaymentSchedule.getProcessId());
                }
            }
            if (StringUtils.isNotBlank(repaymentSchedule.getProcessId()) && repaymentSchedule.getProcessId().length() > 10) {
                request.setAttribute("processId", repaymentSchedule.getProcessId());
                String auditTabFlag = capProcessManageService.checkShowAuditTab(repaymentSchedule.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), repaymentSchedule.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }

        }
        return "cap/lay-RepaymentPlan-detail";
    }

    /*借款合同详情  id是列表带过的 */
    @RequestMapping("/queryDetail")
    @ResponseBody
    public RepaymentScheduleDto queryDetail(String id) {
        RepaymentScheduleDto dto = repaymentScheduleService.queryDetail(id);
        return dto;
    }


    @RequestMapping("/queryDetailByRepay")
    @ResponseBody
    public RspVo queryDetailByRepay(String id){
        return  repaymentScheduleService.queryDetailByRepay(id);
    }

    /*
     *  详情下拉数据
     * */
    @RequestMapping("queryProject")
    @ResponseBody
    public RspVo queryProject(){
        //查找已通过合同评审的项目
        List<DropLoanFinDomain> dropLoanFinDomain =  loanContractService.selectProject();
        return RspVo.getSuccessResponse(dropLoanFinDomain);
    }

    //选择项目之后金融机构的下拉
    @RequestMapping("/queryLoan")
    @ResponseBody
    public RspVo queryLoan(String id){
        //使用id查找对应的金融机构
        List<RepaymentScheduleInfoVo> sublists = loanContractService.selectDockById(id);
        return RspVo.getSuccessResponse(sublists);
    }

    @RequestMapping("/save")
    @ResponseBody
    public RspVo save (String bodyStr,Integer submitType){
        AuthorizationVO userDtl = SessionUserUtils.getSessionAttributeForUserDtl();
        String s = StringEscapeUtils.unescapeJava(bodyStr);
        RepaymentScheduleVo vo = JSON.parseObject(s, RepaymentScheduleVo.class);
        RepaymentSchedule repaymentSchedule = new RepaymentSchedule();
        if (StringUtils.isNotBlank(vo.getInfoVo().getId())){ //编辑
            repaymentSchedule.setId(vo.getInfoVo().getId());
            repaymentSchedule.setSetupFinancialId(vo.getInfoVo().getSetupFinancialId());
            repaymentSchedule.setProCode(vo.getInfoVo().getProCode());
            repaymentSchedule.setProName(vo.getInfoVo().getProName());
            repaymentSchedule.setUptBy(userDtl.getId());
            repaymentSchedule.setUptTime(new Date());
        }else{//新增
            repaymentSchedule.setAddBy(userDtl.getId());
            repaymentSchedule.setAddTime(new Date());
            repaymentSchedule.setProCode(vo.getInfoVo().getProCode());
            repaymentSchedule.setProName(vo.getInfoVo().getProName());
            repaymentSchedule.setSetupFinancialId(vo.getInfoVo().getSetupFinancialId());
        }
        return repaymentScheduleService.saveAll(userDtl,bodyStr,submitType,repaymentSchedule,vo.getSublists());
    }
}
